/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.IndexedArray;
import com.bourre.data.iterator.Iterator;

/**
 * Fonts repository.
 * 
 * @author Romain Ecarnot
 */
class fever.data.libs.FontLibLocator
{
	//-------------------------------------------------------------------------
	// Constants definition
	//-------------------------------------------------------------------------
	
	/** Retreives all available fonts. */
	public static var ALL_FONTS : Number = 0;
	
	/** Retreives font system. */
	public static var SYSTEM_FONTS : Number = 1;
	
	/** Retreives embeded font. */
	public static var EMBEDED_FONTS : Number = 2;
	
		
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _a : IndexedArray = new IndexedArray();
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns {@code true} if passed-in {@code name} font is registred in locator.
	 */
	public static function isRegistered( name : String ) : Boolean
	{
		return _a.objectExists( name );
	}
	
	/**
	 * Registers passed-in {@code name} font.
	 */
	public static function register( name : String ) : Void
	{
		if ( !_a.objectExists( name ) ) _a.push( name );
	}
	
	/**
	 * Unregisters {@link fever.data.libs.FontLib} registred with passed-in 
	 * {@code sName} id.
	 */
	public static function unregister( name : String ) : Void
	{
		_a.remove( name );
	}
	
	/**
	 * Returns available font list in current application.
	 * 
	 * @param type Font list filter, valid value is : 
	 * <ul>
	 *   <li>{@link #ALL_FONTS} ( default )</li>
	 *   <li>{@link #SYSTEM_FONTS}</li>
	 *   <li>{@link #EMBEDED_FONTS}</li>
	 * </ul>
	 * 
	 * @return Array 
	 */
	public static function getFontList( type : Number ) : Array
	{
		switch( type )
		{
			case SYSTEM_FONTS : return _getSystemFontList();
			case EMBEDED_FONTS : return _getEmbededFontList();
			default	: return _getFullFontList();
		}
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private static function _getSystemFontList() : Array
	{
		return TextField.getFontList().sort( Array.CASEINSENSITIVE );	
	}
	
	private static function _getEmbededFontList() : Array
	{
		var it : Iterator = _a.getIterator();
		var list : Array = new Array();
		
		while( it.hasNext() ) list.push( it.next() );
		list.sort( Array.CASEINSENSITIVE );
		return list;
	}
	
	private static function _getFullFontList() : Array
	{
		var list : Array = _getSystemFontList().concat( _getEmbededFontList() );
		list.sort( Array.CASEINSENSITIVE );
		return list;
	}
}